<?php

use common\helpers\Html;
use common\helpers\Url;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '采购订单';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title"><?= Html::encode($this->title) ?></h3>
                <div class="box-tools">
                    <?= Html::a('通过审核', "javascript:void(0);", ['class' => 'btn btn-success btn-sm putaway-all']); ?>
                    <?= Html::create(['edit']) ?>
                </div>
            </div>
            <div class="box-body table-responsive">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'tableOptions' => ['class' => 'table table-hover'],
        'options' => [
            'id' => 'grid',
        ],
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'checkboxOptions' => function ($model, $key, $index, $column) {
                    return ['value' => $model->id];
                },
            ],
            [
                'class' => 'yii\grid\SerialColumn',
                'visible' => false,
            ],

            'id',
            'number',
            //'supplier_id',
            'linkman',
            'linkmobile',
            'delivery_date',
            //'store_id',
            [
                'label' => '类型',
                'filter' => false, //不显示搜索框
                'value' => function ($model) {
                    $type_array = [ '1' => '买家自提', '2' => '卖方发货', '3' => '代发' ];
                    return $type_array[$model->type];
                },
                'format' => 'raw',
            ],
            'billing_date',
            [
                'label' => '订单状态',
                'filter' => false, //不显示搜索框
                'value' => function ($model) {
                    $status_array = [ '1' => '待审核', '2' => '待入库', '3' => '已入库' ];
                    return $status_array[$model->status];
                },
                'format' => 'raw',
            ],
            [
                'label' => '创建时间',
                'filter' => false,
                'value' => function ($model) {
                    return Yii::$app->formatter->asDatetime($model->created_at);
                },
                'format' => 'raw',
            ],
            //'updated_at',
            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{edit} {status} {delete}',
                'buttons' => [
                'edit' => function($url, $model, $key){
                        return Html::edit(['edit', 'id' => $model->id]);
                },
               'status' => function($url, $model, $key){
                        return Html::status($model['status']);
                  },
                'delete' => function($url, $model, $key){
                        return Html::delete(['delete', 'id' => $model->id]);
                },
                ]
            ]
    ]
    ]); ?>
            </div>
        </div>
    </div>
</div>

<script>
    // 通过审批
    $(document).on("click",".putaway-all",function(){
        url = "<?= Url::to(['approved', 'state' => true])?>";
        sendData(url);
    });

    function sendData(url, ids = []) {
        if (ids.length === 0) {
            ids = $("#grid").yiiGridView("getSelectedRows");
        }
        $.ajax({
            type: "post",
            url: url,
            dataType: "json",
            data: {ids: ids},
            success: function (data) {
                if (parseInt(data.code) === 200) {
                    swal('小手一抖打开一个窗', {
                        buttons: {
                            defeat: '确定',
                        },
                        title: '操作成功',
                    }).then(function (value) {
                        switch (value) {
                            case "defeat":
                                location.reload();
                                break;
                            default:
                        }
                    });
                } else {
                    rfWarning(data.message);
                }
            }
        });
    }
</script>
